#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import io
import sys
import django

# 添加项目路径到系统路径
backend_path = os.path.join(os.path.dirname(__file__), '..', 'backend')
sys.path.append(backend_path)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'massage_app.settings')

# 初始化Django
django.setup()

from backend.tools.minio_client import minio_client
import cairosvg

# 创建临时目录用于存储生成的图片
temp_dir = "temp_images"
os.makedirs(temp_dir, exist_ok=True)

# SVG模板
svg_templates = {
    "banner1": '''<svg width="600" height="300" xmlns="http://www.w3.org/2000/svg">
        <rect width="100%" height="100%" fill="#4a90e2"/>
        <text x="50%" y="50%" font-family="Arial" font-size="32" fill="white" text-anchor="middle" dominant-baseline="middle">按摩服务Banner 1</text>
    </svg>''',
    
    "banner2": '''<svg width="600" height="300" xmlns="http://www.w3.org/2000/svg">
        <rect width="100%" height="100%" fill="#e24a6a"/>
        <text x="50%" y="50%" font-family="Arial" font-size="32" fill="white" text-anchor="middle" dominant-baseline="middle">按摩服务Banner 2</text>
    </svg>''',
    
    "massage": '''<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="40" r="25" fill="#888"/>
        <rect x="30" y="70" width="40" height="20" fill="#888" rx="5"/>
        <text x="50" y="95" font-family="Arial" font-size="12" fill="#666" text-anchor="middle">按摩</text>
    </svg>''',
    
    "spa": '''<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="40" r="25" fill="#4a90e2"/>
        <rect x="30" y="70" width="40" height="20" fill="#4a90e2" rx="5"/>
        <text x="50" y="95" font-family="Arial" font-size="12" fill="#333" text-anchor="middle">SPA</text>
    </svg>''',
    
    "footcare": '''<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="40" r="25" fill="#e24a6a"/>
        <rect x="30" y="70" width="40" height="20" fill="#e24a6a" rx="5"/>
        <text x="50" y="95" font-family="Arial" font-size="12" fill="#333" text-anchor="middle">足疗</text>
    </svg>''',
    
    "bodycare": '''<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="40" r="25" fill="#7ed321"/>
        <rect x="30" y="70" width="40" height="20" fill="#7ed321" rx="5"/>
        <text x="50" y="95" font-family="Arial" font-size="12" fill="#333" text-anchor="middle">身体护理</text>
    </svg>''',
    
    "technician1": '''<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
        <circle cx="100" cy="70" r="40" fill="#ccc"/>
        <rect x="60" y="120" width="80" height="60" fill="#ccc" rx="10"/>
        <text x="100" y="190" font-family="Arial" font-size="16" fill="#999" text-anchor="middle">技师1</text>
    </svg>''',
    
    "technician2": '''<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
        <circle cx="100" cy="70" r="40" fill="#bbb"/>
        <rect x="60" y="120" width="80" height="60" fill="#bbb" rx="10"/>
        <text x="100" y="190" font-family="Arial" font-size="16" fill="#888" text-anchor="middle">技师2</text>
    </svg>''',
    
    "technician3": '''<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
        <circle cx="100" cy="70" r="40" fill="#aaa"/>
        <rect x="60" y="120" width="80" height="60" fill="#aaa" rx="10"/>
        <text x="100" y="190" font-family="Arial" font-size="16" fill="#777" text-anchor="middle">技师3</text>
    </svg>''',
    
    "service1": '''<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
        <rect x="20" y="20" width="160" height="160" fill="#3cc51f" rx="10"/>
        <circle cx="100" cy="100" r="30" fill="white" opacity="0.5"/>
        <text x="100" y="180" font-family="Arial" font-size="16" fill="white" text-anchor="middle">服务1</text>
    </svg>''',
    
    "service2": '''<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
        <rect x="20" y="20" width="160" height="160" fill="#4a90e2" rx="10"/>
        <circle cx="100" cy="100" r="30" fill="white" opacity="0.5"/>
        <text x="100" y="180" font-family="Arial" font-size="16" fill="white" text-anchor="middle">服务2</text>
    </svg>'''
}

def generate_and_upload_images():
    """生成图片并上传到MinIO"""
    image_urls = {}
    
    for name, svg_content in svg_templates.items():
        print(f"处理图片: {name}")
        
        try:
            # 使用cairosvg将SVG转换为PNG
            png_data = cairosvg.svg2png(bytestring=svg_content.encode('utf-8'))
            
            # 上传到MinIO
            object_name = f"images/{name}.png"
            minio_client.upload_bytes(object_name, png_data, content_type="image/png")
            url = minio_client.presigned_get_url(object_name)
            image_urls[name] = url
            print(f"上传成功: {name} -> {url}")
        except Exception as e:
            print(f"处理失败: {name}, 错误: {e}")
    
    return image_urls

if __name__ == "__main__":
    urls = generate_and_upload_images()
    print("\n生成的图片URL:")
    for name, url in urls.items():
        print(f"{name}: {url}")